package cn.hy.spider;

import cn.hutool.core.collection.CollUtil;

import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.*;

/**
 * 首先从顶部分类标题开始，依次爬取每一个标题（小分页），每一个标题（大分页。）
 */
public class PictureSpider {

    private final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);

    private volatile boolean isStop = false;

    /**
     * 开始爬虫爬取！
     */
    public void start(Collection<Picture> pictures) throws IOException {
        if (isStop || CollUtil.isEmpty(pictures)) {
            return;
        }
        for (Picture picture : pictures) {
            Future<?> future = executorService.submit(new Runnable() {
                @Override
                public void run() {
                    cn.hy.spider.SinglePictureDownloader singlePictureDownloader = new SinglePictureDownloader(picture, "", picture.getFilePath());
                    singlePictureDownloader.download();
                }
            });
            try {
                future.get(5, TimeUnit.SECONDS);
            } catch (InterruptedException | TimeoutException | ExecutionException e) {
                e.printStackTrace();
                future.cancel(true);
            }
        }

    }

    public void stop() {
        isStop = true;
        executorService.shutdown();
    }
}